package com.yuncheng.spcyApi.webOffice;

import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.entity.SpcyZltxwjBasic;
import com.yuncheng.spcyApi.entity.SpcyZltxwjFlow;
import com.yuncheng.spcyApi.service.ISpcyZltxwjBasicService;
import com.yuncheng.spcyApi.service.ISpcyZltxwjFlowService;
import com.yuncheng.spcyApi.utils.HnCaUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "质量体系文件-WebOffice在线文档编辑接口")
@RestController
@RequestMapping("/api/spzx-spcy/ZltxwjWebOfficeApi")
public class ZltxwjWebOfficeApi {
    private static final Logger log = LoggerFactory.getLogger(ZltxwjWebOfficeApi.class);

    @Resource
    private WebOfficeFile webOfficeFIle;

    @Resource
    private ISpcyZltxwjFlowService spcyZltxwjFlowService;

    @Resource
    private ISpcyZltxwjBasicService spcyZltxwjBasicService;

    @Resource
    @Lazy
    private HnCaUtils hnCaUtils;

    @Log(title = "服务端-打开在线编辑文档-流程文件")
    @ApiOperation(value = "服务端-打开在线编辑文档-流程文件")
    @ApiImplicitParam(name = "id",value = "质量体系文件id")
    @GetMapping(value = "openFile")
    public HttpResult<?> openFile(@RequestParam(value = "id",defaultValue = "") String id){
        /*if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyZltxwjFlow wjjl = spcyZltxwjFlowService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在！");
        }

        if (StringUtils.isBlank(wjjl.getFwjlj())){
            return HttpResult.error("文件不存在！");
        }

        if (StringUtils.isNotBlank(wjjl.getFwjlx())){
            if (!(wjjl.getFwjlx().equals(".doc") || wjjl.getFwjlx().equals(".docx"))){
                return HttpResult.error("只有文档格式的文件才支持查看！");
            }
        }

        String url = webOfficeFIle.openFileByZltxwj(wjjl,wjjl.getId(),wjjl.getFwjlj());*/

        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

    @Log(title = "服务端-打开只读在线文档-流程文件")
    @ApiOperation(value = "服务端-打开只读在线文档-流程文件")
    @ApiImplicitParam(name = "id",value = "质量体系文件id")
    @GetMapping(value = "onlineSeeOpenFile")
    public HttpResult<?> onlineSeeOpenFile(@RequestParam(value = "id",defaultValue = "") String id){
        /*if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyZltxwjFlow wjjl = spcyZltxwjFlowService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在！");
        }
        if (StringUtils.isBlank(wjjl.getFwjlj())){
            return HttpResult.error("文件不存在！");
        }
        if (StringUtils.isNotBlank(wjjl.getFwjlx())){
            if (!(wjjl.getFwjlx().equals(".doc") || wjjl.getFwjlx().equals(".docx"))){
                return HttpResult.error("只有文档格式的文件才支持查看！");
            }
        }

        String url = webOfficeFIle.openSeeFileByZltxwj(wjjl, wjjl.getId(), wjjl.getFwjlj());*/

        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

    @Log(title = "服务端-打开在线文档-资源库")
    @ApiOperation(value = "服务端-打开在线文档-资源库")
    @ApiImplicitParam(name = "id",value = "质量体系文件id")
    @GetMapping(value = "openFileBase")
    public HttpResult<?> openFileBase(@RequestParam(value = "id",defaultValue = "") String id){
        /*if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyZltxwjBasic wjjl = spcyZltxwjBasicService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在！");
        }
        if (StringUtils.isBlank(wjjl.getFwjlj())){
            return HttpResult.error("文件不存在！");
        }

        if (StringUtils.isNotBlank(wjjl.getFwjlx())){
            if (!(wjjl.getFwjlx().equals(".doc") || wjjl.getFwjlx().equals(".docx"))){
                return HttpResult.error("只有文档格式的文件才支持查看！");
            }
        }


        String url = webOfficeFIle.openFileBaseByZltxwj(wjjl, wjjl.getId(), wjjl.getFwjlj());*/

        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

    @Log(title = "服务端-将word文件关闭并保存-流程文件")
    @ApiOperation(value = "服务端-将word文件关闭并保存-流程文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "质量体系文件id",required = true),
            @ApiImplicitParam(name = "isClose",value = "是否关闭文档，true=【关闭】")
    })
    @GetMapping(value = "getWebOfficeDownloadFile")
    public HttpResult<?> getWebOfficeDownloadFile(@RequestParam(value = "id",defaultValue = "") String id,
                                                  @RequestParam(value = "isClose",defaultValue = "false") boolean isClose){
        /*boolean isDel = false;
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyZltxwjFlow wjjl = spcyZltxwjFlowService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在");
        }

        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.ok("保存关闭成功");
        }

        // 关闭文档
        Result closeResult1 = webOfficeFIle.closeFile(id);
        if (!closeResult1.isSuccess()){
            return HttpResult.error(closeResult1.getMessage());
        }

        webOfficeFIle.getWebOfficeDownloadFile2ByZltxwj(wjjl, wjjl.getFwjlj(), isDel);

        // 关闭文档
        if (isClose) {
            return HttpResult.ok("保存关闭成功");
        }else {

            return HttpResult.ok("保存成功");
        }*/

        return HttpResult.ok("服务不可用！");
    }

    @Log(title = "服务端-流程文件-生成pdf")
    @ApiOperation(value = "服务端-流程文件-生成pdf")
    @GetMapping(value = "fileConvertPdf")
    public HttpResult fileConvertPdf(@RequestParam(value = "id", defaultValue = "") String id){

        /*if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyZltxwjFlow wjjl = spcyZltxwjFlowService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在！");
        }

        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.ok("pdf无法再次转换！");
        }

        // 关闭文档
        Result closeResult1 = webOfficeFIle.closeFile(id);
        if (!closeResult1.isSuccess()){
            return HttpResult.error(closeResult1.getMessage());
        }

        webOfficeFIle.getWebOfficeDownloadFile2ByZltxwj(wjjl, wjjl.getFwjlj(), false);

        // 转为pdf
        hnCaUtils.zltxwjFlowFileConvertPdf(id);

        return HttpResult.ok("生成成功");*/
        return HttpResult.ok("服务不可用！");
    }

    @Log(title = "服务端-流程文件-重新编辑")
    @ApiOperation(value = "服务端-流程文件-重新编辑")
    @GetMapping(value = "againEditFlowFile")
    public HttpResult againEditFlowFile(@RequestParam(value = "id", defaultValue = "") String id){
        /*if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyZltxwjFlow wjjl = spcyZltxwjFlowService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在！");
        }

        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && !wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.ok("只有pdf能够重新编辑！");
        }

        SpcyZltxwjFlow zltxwjFlow = webOfficeFIle.againEditZltxwj(wjjl);
        String url = webOfficeFIle.openFileByZltxwj(zltxwjFlow, zltxwjFlow.getId(), zltxwjFlow.getFwjlj());*/

        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

}
